package com.seecen.controller;

import com.seecen.pojo.SeekComment;
import com.seecen.pojo.Seeking;
import com.seecen.service.FileService;
import com.seecen.service.SeekCommentService;
import com.seecen.service.SeekingService;
import com.seecen.utils.Result;
import jakarta.annotation.Resource;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.*;

@RestController
@RequestMapping("/seek")
@CrossOrigin
public class SeekController {

    @Autowired
    private SeekingService seekingService;
    @Autowired
    SeekCommentService seekCommentService;

    @Autowired
    private FileService fileService;


    @PostMapping("publishSeeking")
    public Result publishSeeking(@RequestBody Seeking seeking){
        Result ret = seekingService.publishSeeking(seeking);
        return Result.ok(null);
    }

    @GetMapping("getSeeking")
    public Result getSeeking(){

        Result ret = seekingService.getSeeking();
         return ret;
    }
    @PostMapping("getSeekById")
    public Result getSeekById(@RequestParam Integer id){
        Result ret = seekingService.getSeekById(id);
        return ret;
    }
    @PostMapping("getSeekCommentById")
    public Result getSeekCommentById(@RequestParam Integer id){
        Result ret = seekCommentService.getSeekCommentById(id);
        return ret;
    }
    @PostMapping("publishComment")
    public Result publishComment(@RequestBody SeekComment seekComment){
        Result ret = seekCommentService.publishComment(seekComment);
        return ret;
    }


    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/publish")
    public ResponseEntity<Map<String, Object>>  publishSeek(
            @RequestPart("images") MultipartFile[] files,
            @RequestParam String seekContent,
            @RequestParam String position,
            @RequestParam String seekAutorName,
            @RequestParam Integer seekAutorId,
            @RequestParam(required = false) Double reward) {
        // 处理图片上传
        String imagePaths = fileService.saveFiles(files);

        // 保存到数据库
        Seeking seeking = new Seeking();
        seeking.setSeekContent(seekContent);
        seeking.setPosition(position);
        seeking.setSeekAutorId(seekAutorId);
        seeking.setSeekAutorName(seekAutorName);
        //  entity.setReward(reward);
        seeking.setSeekImgUrl(imagePaths);
        seeking.setSeekCreateTime(new Date());

        Result ret = seekingService.saveSeek(seeking);
        return ResponseEntity.ok()
                .body(Map.of("code", 200,
                        "message", "上传成功"
                        ));
    }




//    @PostMapping("/publish1")
//    public ResponseEntity<Map<String, Object>> uploadFiles(
//            @RequestParam("images") MultipartFile[] files) {
//
//
//        if(files == null || files.length == 0) {
//            return ResponseEntity.badRequest()
//                    .body(Map.of("success", false, "message", "未选择文件"));
//        }
//
//        try {
//            Path uploadDir = Paths.get("./uploads");
//            if(!Files.exists(uploadDir)) {
//                Files.createDirectories(uploadDir);
//            }
//
//            List<String> savedFiles = new ArrayList<>();
//            for(MultipartFile file : files) {
//                String originalName = file.getOriginalFilename();
//                String extension = originalName.substring(originalName.lastIndexOf("."));
//                String newFileName = UUID.randomUUID() + extension;
//
//                Path targetPath = uploadDir.resolve(newFileName);
//                Files.copy(file.getInputStream(), targetPath,
//                        StandardCopyOption.REPLACE_EXISTING);
//                savedFiles.add(newFileName);
//            }
//
//            return ResponseEntity.ok()
//                    .body(Map.of("success", true,
//                            "message", "上传成功",
//                            "files", savedFiles));
//        } catch (Exception e) {
//            return ResponseEntity.internalServerError()
//                    .body(Map.of("success", false,
//                            "message", "上传失败: " + e.getMessage()));
//        }
//    }
}
